package im.starchat.chat.activity.publish;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.rest.model.RoomBean;
import im.starchat.chat.rest.model.api.NullResponse;
import im.starchat.chat.utils.Constants;
import im.starchat.chat.utils.Util;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * 直播结束界面
 */
public class GameOverActivity extends Activity {
    public static String TAG = "GameOverActivity";
    private TextView mPraiseCountTextView;
    private TextView mViewerCountTextView;
    private int roomNum;
    private int ducation;
    private boolean hostleave = false;
    private Context ctx;
    @InjectView(R.id.star_layout)
    LinearLayout mStarLayout;
    @InjectView(R.id.user_layout)
    LinearLayout mUserLayout;
    @InjectView(R.id.viewercount)
    TextView mUserViewCount;

    @InjectView(R.id.viewercount_star)
    TextView mStarViewCount;

    @InjectView(R.id.popularity_star)
    TextView mStarPopularCount;
    @InjectView(R.id.du_star)
    TextView mStarDuCount;
    @InjectView(R.id.like_star)
    TextView mStarLike;
    @InjectView(R.id.commit)
    Button mCommit;
    @InjectView(R.id.attention)
    TextView mAttention;
    @InjectView(R.id.cancel_attention)
    TextView mCancelAttention;

    private String mHostIdentifier = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_over_activity);
        ButterKnife.inject(this);

        ctx = this;
        //恢复变量

        mViewerCountTextView = (TextView) findViewById(R.id.viewercount);
        mPraiseCountTextView = (TextView) findViewById(R.id.praisecount);
        TextView title = (TextView) findViewById(R.id.title);
        roomNum = getIntent().getExtras().getInt(Util.EXTRA_ROOM_NUM);
        hostleave = getIntent().getExtras().getBoolean(Util.EXTRA_LEAVE_MODE);
        mHostIdentifier = getIntent().getExtras().getString(Util.EXTRA_USER_ID, "");
        ducation = getIntent().getExtras().getInt(Util.EXTRA_RECORDTIME, 0);
        if (hostleave == true) {
            title.setText("主播已离开");
        }
        getLiveInfo();
        ((Button) findViewById(R.id.return_main)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if(Constants.isCreater){
            mStarLayout.setVisibility(View.VISIBLE);
            mUserLayout.setVisibility(View.GONE);
        }else{
            mStarLayout.setVisibility(View.GONE);
            mUserLayout.setVisibility(View.VISIBLE);
        }
        mCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mAttention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                following(mHostIdentifier);
            }
        });
        mCancelAttention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelFollowing(mHostIdentifier);
            }
        });

    }

    public void getLiveInfo() {

        XLApplication.getInstance().getRestClient().getFeedInfo(roomNum + "", new Callback<RoomBean>() {
            @Override
            public void success(RoomBean roomBean, Response response) {

                if(Constants.isCreater){
                    mStarViewCount.setText(roomBean.getWatchers_count() + "");
                    mStarPopularCount.setText(roomBean.getPopularity() + "");
                    updateWallTime(ducation);//roomBean.getDuration());
//                    mStarDuCount.setText(roomBean.getDuration() + "");
                    mStarLike.setText(roomBean.getPraises_count() + "");
                }else{
                    mUserViewCount.setText(roomBean.getWatchers_count() + "");
                }
//                if(roomBean.i)


//                mHandler.sendEmptyMessage(SHOW_LIVE_INFO);
                Constants.isCreater = false;
            }

            @Override
            public void failure(RetrofitError error) {
                Constants.isCreater = false;
            }
        });

    }

    private void updateWallTime(int second) {
        String formatTime;
        String hs, ms, ss;

        long h, m, s;
        h = second / 3600;
        m = (second % 3600) / 60;
        s = (second % 3600) % 60;
        if (h < 10) {
            hs = "0" + h;
        } else {
            hs = "" + h;
        }

        if (m < 10) {
            ms = "0" + m;
        } else {
            ms = "" + m;
        }

        if (s < 10) {
            ss = "0" + s;
        } else {
            ss = "" + s;
        }

        formatTime = hs + ":" + ms + ":" + ss;
        mStarDuCount.setText(formatTime);
    }


    private void following(String id){
        if(!TextUtils.isEmpty(id)){
            XLApplication.getInstance().getRestClient().following(id, "", new Callback<NullResponse>() {
                @Override
                public void success(NullResponse nullResponse, Response response) {
                    Toast.makeText(GameOverActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
                    mAttention.setVisibility(View.GONE);
                    mCancelAttention.setVisibility(View.VISIBLE);
                }

                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(GameOverActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
                }
            });
        }else{
            Toast.makeText(GameOverActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
        }

    }

    private void cancelFollowing(String id){
        XLApplication.getInstance().getRestClient().delFollowing(id, new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                Toast.makeText(GameOverActivity.this, "取消关注成功", Toast.LENGTH_SHORT).show();
                mAttention.setVisibility(View.VISIBLE);
                mCancelAttention.setVisibility(View.GONE);
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

}
